package com.hison.controller;

import com.hison.domian.dto.AddArticleDto;
import com.hison.domian.dto.ArticleDto;
import com.hison.domian.entity.Article;
import com.hison.domian.vo.ArticleByIdVo;
import com.hison.domian.vo.PageVo;
import com.hison.service.ArticleService;
import com.hison.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName: ArticleController
 * @Descripiton: TODO
 * @Author: 小郑
 * @Date： 2024/1/14 21:57
 * @Version：1.8
 **/
@RestController
@RequestMapping("/content/article")
public class ArticleController {
    //------------------------------新增博客文章-----------------------------

    @Autowired
    private ArticleService articleService;

    /**
     * 新增文章
     * @param article
     * @return {@link ResponseResult}
     */
    @PostMapping
    public ResponseResult add(@RequestBody AddArticleDto article){
        return articleService.add(article);
    }

    //-----------------------------分页查询博客文章---------------------------

    @GetMapping("/list")
    public ResponseResult list(Article article, Integer pageNum, Integer pageSize){
        PageVo pageVo = articleService.selectArticlePage(article,pageNum,pageSize);
        return ResponseResult.okResult(pageVo);
    }


    //---------------------------根据文章id来修改文章--------------------------

    @GetMapping(value = "/{id}")
    //①先查询根据文章id查询对应的文章
    public ResponseResult getInfo(@PathVariable(value = "id")Long id){
        ArticleByIdVo article = articleService.getInfo(id);
        return ResponseResult.okResult(article);
    }

    @PutMapping
    //②然后才是修改文章
    public ResponseResult edit(@RequestBody ArticleDto article){
        articleService.edit(article);
        return ResponseResult.okResult();
    }

    //---------------------------根据文章id来删除文章-------------------------

    /**
     * 根据文章id删除文章
     * @param id
     * @return {@link ResponseResult}
     */
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable Long id){
        //删除文章使用逻辑删除

        return  articleService.deleteByID(id);
    }
}